package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.entity.Address;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.dao.impl
 * @Date 2023/4/14 21:25
 */
public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> queryAddressByUid(Integer uid) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            return qr.query("select * from tb_address where uid=?",new BeanListHandler<>(Address.class),uid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void addAddress(Address address) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        String sql="insert into tb_address values (?,?,?,?,?,?)";
        Object[] params={address.getId(),address.getDetail(),address.getName(),address.getPhone(),address.getUid(),address.getLevel()};
        try {
            qr.update(sql,params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void deleteAddressById(int intId) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            qr.update("delete from tb_address where id=?",intId);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void updateAddress(Address address) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        Object[] params={address.getDetail(),address.getName(),address.getPhone(),address.getLevel(),address.getId(),address.getUid()};
        try {
            qr.update("update tb_address set detail=?,name=?,phone=? ,level=? where id=? and uid=?",params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void updateAddressAllLevel(int uid) {
        QueryRunner qr=new QueryRunner();
        Connection conn = DruidUtils.getConnection();
        try {
            qr.update(conn,"update tb_address set level=0 where uid=?",uid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }finally {
            DruidUtils.closeAll(null,null,conn);
        }
    }

    @Override
    public void updateAddressLevel(int id) {
        QueryRunner qr=new QueryRunner();
        Connection conn = DruidUtils.getConnection();
        try {
            qr.update(conn,"update tb_address set level=1 where id=?",id);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }finally {
            DruidUtils.closeAll(null,null,conn);
        }
    }
}
